<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  

  
  <title>JavaScript基础知识点--DOM属性 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="认识属性HTML 属性分为两种  property：固有属性  attribute：自定义属性   属性说明 Ele.attributes：返回指定节点的属性集合  Ele.attributes.getNamedItem(“attributeName”).nodeValue：返回指定的属性节点。attributeName为要查找的属性值的属性名，该方法只能获取在元素上定义的属性值  Ele.at">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript基础知识点--DOM属性">
<meta property="og:url" content="http://yoursite.com/2019/08/31/JavaScript基础知识点-DOM属性/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="认识属性HTML 属性分为两种  property：固有属性  attribute：自定义属性   属性说明 Ele.attributes：返回指定节点的属性集合  Ele.attributes.getNamedItem(“attributeName”).nodeValue：返回指定的属性节点。attributeName为要查找的属性值的属性名，该方法只能获取在元素上定义的属性值  Ele.at">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-08-31T10:04:53.806Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript基础知识点--DOM属性">
<meta name="twitter:description" content="认识属性HTML 属性分为两种  property：固有属性  attribute：自定义属性   属性说明 Ele.attributes：返回指定节点的属性集合  Ele.attributes.getNamedItem(“attributeName”).nodeValue：返回指定的属性节点。attributeName为要查找的属性值的属性名，该方法只能获取在元素上定义的属性值  Ele.at">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-JavaScript基础知识点-DOM属性" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2019/08/31/JavaScript基础知识点-DOM属性/" class="article-date">
  <time datetime="2019-08-31T01:34:26.000Z" itemprop="datePublished">2019-08-31</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JavaScript基础知识点--DOM属性
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <hr>
<h1 id="认识属性"><a href="#认识属性" class="headerlink" title="认识属性"></a>认识属性</h1><p>HTML 属性分为两种</p>
<ol>
<li><p>property：固有属性</p>
</li>
<li><p>attribute：自定义属性</p>
</li>
</ol>
<h2 id="属性说明"><a href="#属性说明" class="headerlink" title="属性说明"></a>属性说明</h2><ul>
<li><p>Ele.attributes：返回指定节点的属性集合</p>
</li>
<li><p>Ele.attributes.getNamedItem(“attributeName”).nodeValue：返回指定的属性节点。attributeName为要查找的属性值的属性名，该方法只能获取在元素上定义的属性值</p>
</li>
<li><p>Ele.attributes.removeNamedItem(“attributeName”)：移除指定的属性节点</p>
</li>
<li><p>Ele.attributes.setNamedItem(“attribute”)：将已有的属性节点设置给该元素</p>
</li>
<li><p>document.createAttribute(“arributeName”)：在文档中创建一个属性节点。若是要为指定元素添加属性，需要到 setNamedItem() 方法或 setAttributeNode() 方法</p>
</li>
<li><p>nodeName：会根据所属节点类型返回节点的名称</p>
<table>
<thead>
<tr>
<th>nodeName所属节点的类型</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>元素节点</td>
<td>标签名称</td>
</tr>
<tr>
<td>属性节点</td>
<td>属性名称</td>
</tr>
<tr>
<td>文本节点</td>
<td>#text</td>
</tr>
<tr>
<td>文档节点(document)</td>
<td>#document</td>
</tr>
</tbody></table>
</li>
<li><p>nodeValue：会根据所属节点类型返回节点的值。nodeValue 属性对于文档节点和元素节点是不可用的</p>
<table>
<thead>
<tr>
<th>nodeValue所属节点的类型</th>
<th>返回值</th>
</tr>
</thead>
<tbody><tr>
<td>文本节点</td>
<td>文本</td>
</tr>
<tr>
<td>属性节点</td>
<td>属性值</td>
</tr>
</tbody></table>
</li>
<li><p>nodeType：返回一个数值代表节点的类型</p>
<table>
<thead>
<tr>
<th>接口</th>
<th>nodeType常量</th>
<th>nodeType值</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>Element</td>
<td>Node.ELEMENT_NODE</td>
<td>1</td>
<td>元素节点</td>
</tr>
<tr>
<td>Text</td>
<td>Node.TEXT_NODE</td>
<td>3</td>
<td>文本节点</td>
</tr>
<tr>
<td>Document</td>
<td>Node.DOCUMENT_NODE</td>
<td>9</td>
<td>document</td>
</tr>
<tr>
<td>Comment</td>
<td>Node.COMMENT_NODE</td>
<td>8</td>
<td>注释的文本</td>
</tr>
<tr>
<td>DocumentFragment</td>
<td>Node.DOCUMENT_FRAGMENT_NODE</td>
<td>11</td>
<td>文档片段</td>
</tr>
<tr>
<td>Attr</td>
<td>Node.ATTRIBUTE_NODE</td>
<td>2</td>
<td>节点属性</td>
</tr>
</tbody></table>
</li>
<li><p>Ele.getAttribute(“attributeName”)：获取该属性名的属性值。包括固有属性和自定义属性</p>
</li>
<li><p>Ele.setAttribute(attributeName, attributeValue)：添加或修改元素的属性节点的属性值</p>
</li>
<li><p>Ele.removeAttribute(attributeName)：移除该元素指定的属性名及其属性值</p>
</li>
<li><p>Ele.setAttributeNode(attribute)：将属性节点添加到指定元素内</p>
</li>
</ul>
<h1 id="nodeValue-与-value-的区别"><a href="#nodeValue-与-value-的区别" class="headerlink" title="nodeValue 与 value 的区别"></a>nodeValue 与 value 的区别</h1><p>nodeValue 属性根据节点的类型设置或返回节点的值。这里创建了一个属性节点，然后给这个节点添加了值</p>
<p>value 属性用于设置或者返回属性的值。这里是直接属性添加值</p>
<h1 id="为元素添加属性"><a href="#为元素添加属性" class="headerlink" title="为元素添加属性"></a>为元素添加属性</h1><h2 id="第一种方法"><a href="#第一种方法" class="headerlink" title="第一种方法"></a>第一种方法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ele.color.colorName=&quot;colorValue&quot;;</span><br></pre></td></tr></table></figure>

<h2 id="第二种方法"><a href="#第二种方法" class="headerlink" title="第二种方法"></a>第二种方法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ele.setArribute(&quot;arributeName&quot;, &quot;arributeValue&quot;);</span><br></pre></td></tr></table></figure>

<h2 id="第三种方法"><a href="#第三种方法" class="headerlink" title="第三种方法"></a>第三种方法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var arr=document.createAttribute(&quot;arributeName&quot;);// 设置元素节点的属性名</span><br><span class="line">arr.value=&quot;arributeValue&quot;;// 设置元素节点的属性值</span><br><span class="line">Ele.setAttributeNode(arr);// 将元素节点添加到元素中</span><br></pre></td></tr></table></figure>

<h2 id="第四种方法"><a href="#第四种方法" class="headerlink" title="第四种方法"></a>第四种方法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var data = document.createAttribute(&quot;data&quot;);</span><br><span class="line">data.value = &quot;wrap&quot;;</span><br><span class="line">div.attributes.setNamedItem(data);// 为元素添加指定节点</span><br></pre></td></tr></table></figure>

<h1 id="移除属性节点"><a href="#移除属性节点" class="headerlink" title="移除属性节点"></a>移除属性节点</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ele.attributes.removeNamedItem(&quot;attributeName&quot;);</span><br></pre></td></tr></table></figure>

<h1 id="其他属性"><a href="#其他属性" class="headerlink" title="其他属性"></a>其他属性</h1><h2 id="HTML-标签的布尔属性"><a href="#HTML-标签的布尔属性" class="headerlink" title="HTML 标签的布尔属性"></a>HTML 标签的布尔属性</h2><p>如 disabled、multiple、readonly、checked、selected 等属性</p>
<h2 id="字符串属性"><a href="#字符串属性" class="headerlink" title="字符串属性"></a>字符串属性</h2><p>如 id、title（鼠标滑过时的提示信息）、href、src、lang、dir（文本的输出方向）、accesskey（通过快捷键使某个元素快捷选中，快捷键为Alt+定义的键）、name、value、class 等属性</p>
<h3 id="W3C全局属性"><a href="#W3C全局属性" class="headerlink" title="W3C全局属性"></a>W3C全局属性</h3><p>accesskey、class、contenteditable（是否可编辑的内容）、dir、hidden、id、lang、spellcheck、style、tabindex（规定当使用“tab”键进行导航时元素的顺序）、title、translate（规定是否应该翻译元素内容，目前所有主流浏览器都无法正确支持该属性）</p>
<h1 id="classList"><a href="#classList" class="headerlink" title="classList"></a>classList</h1><ul>
<li><p>add(classValue)：为元素添加class类名</p>
</li>
<li><p>remove(classValue)：为元素移除class类名</p>
</li>
<li><p>contain(classValue)：判断该元素是否存在某个class</p>
</li>
<li><p>toggle(classValue)：如果有该classValue，则删除。若没有，则添加上</p>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2019/08/31/JavaScript基础知识点-DOM属性/" data-id="ck4r400qd005dl0w0tpr43dby" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2019/08/31/JavaScript基础知识点-JS事件/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          JavaScript基础知识点--JS事件
        
      </div>
    </a>
  
  
    <a href="/2019/08/26/JavaScript基础知识点-DOM操作之节点/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">JavaScript基础知识点--DOM操作之节点</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Ajax/">Ajax</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/CSS/">CSS</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/CSS3/">CSS3</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Dart/">Dart</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Flutter/">Flutter</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/HTML/">HTML</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/HTML5/">HTML5</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JSON/">JSON</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/QT/">QT</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/React/">React</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Sass/">Sass</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/TheKnowledgeOf/">TheKnowledgeOf</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/TypeScript/">TypeScript</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/bootstrap/">bootstrap</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/jQuery/">jQuery</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Ajax/">Ajax</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CSS/">CSS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/CSS3/">CSS3</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Dart/">Dart</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Flutter/">Flutter</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/">Git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HTML/">HTML</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/HTML5/">HTML5</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JSON/">JSON</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Javascript/">Javascript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/QT/">QT</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React/">React</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Sass/">Sass</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TheKnowledgeOf/">TheKnowledgeOf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/TypeScript/">TypeScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/bootstrap/">bootstrap</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jQuery/">jQuery</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Ajax/" style="font-size: 12.22px;">Ajax</a> <a href="/tags/CSS/" style="font-size: 18.89px;">CSS</a> <a href="/tags/CSS3/" style="font-size: 17.78px;">CSS3</a> <a href="/tags/Dart/" style="font-size: 14.44px;">Dart</a> <a href="/tags/Flutter/" style="font-size: 16.67px;">Flutter</a> <a href="/tags/Git/" style="font-size: 12.22px;">Git</a> <a href="/tags/HTML/" style="font-size: 14.44px;">HTML</a> <a href="/tags/HTML5/" style="font-size: 12.22px;">HTML5</a> <a href="/tags/JSON/" style="font-size: 10px;">JSON</a> <a href="/tags/Java/" style="font-size: 11.11px;">Java</a> <a href="/tags/JavaScript/" style="font-size: 20px;">JavaScript</a> <a href="/tags/Javascript/" style="font-size: 10px;">Javascript</a> <a href="/tags/QT/" style="font-size: 12.22px;">QT</a> <a href="/tags/React/" style="font-size: 15.56px;">React</a> <a href="/tags/Sass/" style="font-size: 11.11px;">Sass</a> <a href="/tags/TheKnowledgeOf/" style="font-size: 11.11px;">TheKnowledgeOf</a> <a href="/tags/TypeScript/" style="font-size: 10px;">TypeScript</a> <a href="/tags/bootstrap/" style="font-size: 11.11px;">bootstrap</a> <a href="/tags/jQuery/" style="font-size: 13.33px;">jQuery</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/12/">December 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">May 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">April 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2019/12/29/Flutter-MaterialApp常用属性介绍/">Flutter-MaterialApp常用属性介绍</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-ListView/">Flutter-ListView</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-AppBar/">Flutter-AppBar</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-TabBar/">Flutter-TabBar</a>
          </li>
        
          <li>
            <a href="/2019/12/29/Flutter-按钮/">Flutter-按钮</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2019 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>